#include "widget.h"

Widget::Widget(QObject *parent)
    : obj(parent)
{
    root = new Item();
#if 0
    /*一级*/
    Item *media = new Item(root, "多媒体");
    Item *navi = new Item( root, "导航",media);
    Item *energy = new Item(1, Item::LEFT_RIGHT, root, "能耗", navi);
    Item *car = new Item(1, Item::LEFT_RIGHT, root, "行车", energy);
    Item *msg = new Item(1, Item::LEFT_RIGHT, root, "整车信息", car, media);
#endif
    /*二级*/
    Item *media = new Item(root, "多媒体");
    Item *navi = new Item( root, "导航", "", media);

    //能耗
    Item *comprehensive_energy = new Item(root, "能耗", "综合油耗", navi);
    Item *small_energy = new Item(root, "能耗", "小计油耗", NULL, comprehensive_energy);
    Item *current_energy = new Item(root, "能耗", "本次油耗", NULL, small_energy);
    Item *add_energy = new Item(root, "能耗", "加油油耗", NULL, current_energy);
    add_energy->setDown(comprehensive_energy);
    comprehensive_energy->isFocus = true;


    //行车
    Item *mainTire_car = new Item(root, "行车", "主车胎压", comprehensive_energy);
    Item *trailerTire_car = new Item(root, "行车", "挂车胎压", NULL, mainTire_car);
    Item *brake_car = new Item(root, "行车", "刹车盘剩余厚度", NULL, trailerTire_car);
    brake_car->setDown(mainTire_car);
    mainTire_car->isFocus = true;

    Item *msg = new Item( root, "整车信息", "个性化设置", mainTire_car);
    msg->isListView = true;
    msg->setRight(media);
    msg->isFocus = true;

    /*三级*/
    //综合油耗
    Item *oil_comprehensive = new Item(comprehensive_energy, "综合油耗", "燃油消耗");
    Item *odo_comprehensive = new Item(comprehensive_energy, "综合油耗", "行驶里程", NULL, oil_comprehensive);
    Item *time_comprehensive = new Item(comprehensive_energy, "综合油耗", "驾驶时间", NULL, odo_comprehensive);
    time_comprehensive->setDown(oil_comprehensive);



    currentItem = root->child;
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    //qDebug() << event->key();
    switch (event->key())
    {
        case Qt::Key_Up:
        {
            qDebug() << "up";
            if(currentItem->up != NULL && currentItem->up!=currentItem)
            {
                currentItem->isFocus = false;
                Item *item = currentItem;
                currentItem = currentItem->up;
                currentItem->isFocus = true;
                updateFocus(item, currentItem);
            }
            break;
        }
        case Qt::Key_Down:
        {
            qDebug() << "Down";
            if(currentItem->down != NULL && currentItem->down != currentItem)
            {
                currentItem->isFocus = false;
                Item *item = currentItem;
                currentItem = currentItem->down;
                currentItem->isFocus = true;
                updateFocus(item, currentItem);
            }
            break;
        }
        case Qt::Key_Left:
        {
            qDebug() << "left";
            /*
            if(currentItem->left != NULL)
            {
                currentItem = currentItem->left;
                updateMenu();
            }*/
            Item *item = currentItem;
            while(item->left == NULL)
            {
                item = item->down;
            }// && item != currentItem

            //if(currentItem->right != NULL)
            {
                item = item->left;
                /*
                if(item->isListView)
                {
                    while(item->isFocus == false)
                    {
                        item = item->down;
                    }
                }*/
                currentItem = item;
                updateMenu();

                if(item->isListView)
                {
                    while(currentItem->isFocus == false)
                    {
                        currentItem = currentItem->down;
                    }
                }
            }
            break;
        }
        case Qt::Key_Right:
        {
            qDebug() << "right";
            Item *item = currentItem;
            while(item->right == NULL)
            {
                item = item->down;
            }// && item != currentItem

            //if(currentItem->right != NULL)
            {
                item = item->right;
/*
                if(item->isListView)
                {
                    while(item->isFocus == false)
                    {
                        item = item->down;
                    }
                }*/

                currentItem = item;

                updateMenu();

                if(item->isListView)
                {
                    while(currentItem->isFocus == false)
                    {
                        currentItem = currentItem->down;
                    }
                }
            }
            break;
        }
        case Qt::Key_Enter:
        case Qt::Key_Return:
        {
            qDebug() << "enter";
            if(currentItem->isListView && currentItem->child!=NULL)
            {
                currentItem = currentItem->child;
                currentItem->isFocus = true;

                currentItem->setLeft(currentItem->parent->left);
                currentItem->setRight(currentItem->parent->right);
                updateMenu();
            }
            break;
        }
        case Qt::Key_Escape:
        {
            qDebug() << "Escape";
            if(currentItem->isListView && currentItem->parent!=root)
            {
                currentItem->isFocus = false;
                while (currentItem->parent == NULL)
                {
                    currentItem = currentItem->down;
                }
                currentItem = currentItem->parent;

                currentItem->setLeft(currentItem->child->left);
                currentItem->setRight(currentItem->child->right);
                updateMenu();

                while(currentItem->isFocus == false)
                {
                    currentItem = currentItem->down;
                }
            }
            break;
        }
        default:
            break;
    }
}

void Widget::updateMenu()
{
    qDebug() << "updateMenu";
    if(currentItem == NULL)
        return ;
    QString title = "";
    QVariantList list;
    bool isListView = false;
#if 0
    if(currentItem->parent == root)//一级菜单
    {
        title = currentItem->title;

        if(!currentItem->data.isEmpty())
        {
            if(currentItem->child)
            {
                Item *item = currentItem->child;
                item->isFocus = true;
                do
                {
                    QVariantMap map;
                    map["isFocus"] = item->isFocus;
                    map["data"] = item->data;
                    qDebug() << "add:" << item->data;

                    item = item->down;
                    list.append(map);
                }while (item != currentItem->child && item!=NULL);
            }
        }
    }
    else
#endif
    {
        title = currentItem->title;
        isListView = currentItem->isListView;

        if(currentItem->isListView)
        {
            //qDebug() << "title:" << title;
            Item *item = currentItem;
            do
            {
                QVariantMap map;
                map["isFocus"] = item->isFocus;
                map["data"] = item->data;

                item = item->down;
                list.append(map);
            }while (item!=NULL && item != currentItem);
        }
    }

    obj->setProperty("title", title);
    obj->setProperty("isListView", isListView);
    if(list.size() > 0)
    {
        //qDebug() << list;
        obj->setProperty("list", list);
        //QMetaObject::invokeMethod(obj, "func", Q_ARG(QVariantList, list));
        //emit sigList(list);
    }
}

void Widget::updateFocus(Item *last, Item *cur)
{
    qDebug() << "updateFocus";
    QVariantList list;
    //Item *item = currentItem;
    {
        QVariantMap map;
        map["isFocus"] = last->isFocus;
        map["data"] = last->data;

        list.append(map);
    }
    {
        QVariantMap map;
        map["isFocus"] = cur->isFocus;
        map["data"] = cur->data;

        list.append(map);
    }

    if(list.size() > 0)
    {
        obj->setProperty("focus", list);
    }
}

void Widget::setObject(QObject *o)
{
    obj = o;
    updateMenu();
}


